package com.shop.interceptor;


import com.shop.constant.UsersConstant;
import com.shop.pojo.ums.Ums_Member;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    //为了方便其他能共享到数据
    public static ThreadLocal<Ums_Member> loginUser=  new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Ums_Member userNameSession =(Ums_Member) request.getSession().getAttribute(UsersConstant.LOGIN_USERS);
        //判断是否登录
        if(userNameSession!=null){
            //登录了就将用户放进去
            loginUser.set(userNameSession);


        }else {
            System.out.println("拦截没登录");

            loginUser.set(null);
        }
        return true;
    }

}
